var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var wxParse = require('../../lib/wxParse/wxParse.js');
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    pagination: {
      page: 1,
      totalPage: 0,
      total: 0
    },
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    placeholder: '点击评论回复...',
    like: false,
    focus: false,
    loadMore: true,
    blogDetail: '',
    disabled: true,
    commentId: 0,
    comments: []
  },
  // 获取活动详情
  blogDetail: function() {
    let that = this;
    util.reqNoAuth(api.BlogDetail, {
      blogId: this.data.blogId
    }).then(function(res) {
      if (res.code == 0) {
        wx.hideLoading();
        that.setData({
          blogDetail: res.data
        })
        wxParse.wxParse('content', 'html', res.data.content, that, 0);
      }
    })
  },
  onPageScroll: function(e) {
    if (e.scrollTop > 100) {
      this.setData({
        backShow: true
      });
    } else {
      this.setData({
        backShow: false
      });
    }
  },
  // 去首页
  toIndex: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  }, sss:function(r){
    //订阅消息
    const version = wx.getSystemInfoSync().SDKVersion
    if (app.compareVersion(version, '2.8.2') >= 0) {
      console.log('开启订阅消息')
      wx.requestSubscribeMessage({
        tmplIds: ['2SjlJxUM-vka1pcfbfSqIs_Uo9ANzQWsi9ScSwyazGQ'],
        success(res) {
          console.log('订阅消息成功:{}', res)
        }, complete(res) {
          console.log('订阅消息回调:{}', res)
        }
      })
    }
  },
  postComment: function(e) {
    let that = this;
    if (!that.data.replyContent) {
      wx.showModal({
        showCancel: false,
        confirmColor: '#1d8f59',
        content: '评论不能为空!'
      });
      return false;
    }
    //订阅消息
    const version = wx.getSystemInfoSync().SDKVersion
    if (app.compareVersion(version, '2.8.2') >= 0) {
      console.log('开启订阅消息')
      wx.requestSubscribeMessage({
        tmplIds: ['2SjlJxUM-vka1pcfbfSqIs_Uo9ANzQWsi9ScSwyazGQ'],
        success(res) {
          console.log('订阅消息成功:{}', res)
        },complete(res){
          console.log('订阅消息回调:{}', res)
        }
      })
    }
    let data = {
      blogId: that.data.blogId,
      commentId: that.data.commentId, //父评论ID，默认为0
      content: that.data.replyContent
    }
    wx.showLoading({
      title: '评论中',
    });
    util.request(api.BlogComment, data, "POST").then(function(res) {
      if (res.code == 0) {
        wx.hideLoading();
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        });
        var pagination = that.data.pagination;
        pagination.page = 1;
        that.setData({
          replyContent: '',
          pagination: pagination,
          comments: [],
          reply_username: '',
          commentId: 0,
          placeholder: '点击评论回复...',
          disabled: true,
          loadMore: true
        });
        that.commentlists();
      }
    });
  },
  toReplyContent: function(e) {
    let that = this;
    let _replyContent = e.detail.value;
    // 禁止输入空格
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    var emptyy = re.test(_replyContent);
    if (emptyy) return false;
    //end
    that.setData({
      replyContent: _replyContent
    })
    if (that.data.replyContent) {
      that.setData({
        disabled: false
      })
    } else {
      that.setData({
        disabled: true
      })
    }
  },
  backComment: function(e) { //回复的评论
    let _from = e.currentTarget.dataset.from;
    let _id = e.currentTarget.dataset.commentid;
    console.log(_id);
    this.setData({
      placeholder: '回复 ' + _from,
      focus: true,
      reply_username: _from,
      commentId: _id
    });
  },
  commentlists: function() {
    let pagination = this.data.pagination;
    if (this.data.loadMore) {
      let that = this;
      util.reqNoAuth(api.BlogCommentList, {
        blogId: that.data.blogId,
        page: pagination.page
      }).then(function(res) {
        if (res.code == 0) {
          let _comments = that.data.comments.concat(res.dataList);
          that.setData({
            comments: _comments,
            pagination: res.pagination
          });
          if (pagination.page >= res.pagination.totalPage) {
            that.setData({
              loadMore: false
            })
          }
        }
      });
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    let pagination = that.data.pagination;
    pagination.page = pagination.page + 1;
    that.setData({
      pagination: pagination
    });
    if (that.data.loadMore) {
      that.commentlists();
    }
  },
  rewardRose: function() {
    var that = this;
    that.setData({
      show: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      blogId: options.blogId || decodeURIComponent(options.scene)
      // blogId: 1
    });
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    this.blogDetail();
    this.commentlists();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: this.data.items.title,
      imageUrl: this.data.blogDetail.imgUrl
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  }
})